package edu.arizona.arid.akshen.entity;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.Vector;

import edu.arizona.arid.akshen.util.StringUtil;


/**
 * Entity class for user
 * 
 * @author Sumin Byeon
 */
public final class User extends Entity {

	/**
	 * Automatically generated serial id
	 */
	private static final long serialVersionUID = -2158885799447218558L;
	
	private int id;
	private String firstname;
	private String lastname;
	private String email;
//	private String password;
	private Access access;
	private int referenceId;
	
	public enum Access {
		Admin, Representative;
		
		/**
		 * Case-insensitive valueOf()
		 * @param access
		 * @return
		 */
		public static Access ivalueOf(String access) {
			return valueOf(StringUtil.capitalize(access));
		}
	}
	
	public User() {
		
	}
	
	public User(ResultSet rs) throws SQLException {
		setId(rs.getInt("userId"));
		setFirstname(rs.getString("firstname"));
		setLastname(rs.getString("lastname"));
		setEmail(rs.getString("email"));
		setAccess(rs.getString("access"));
		setReferenceId(rs.getInt("referenceId"));
	}
	
	public User(String name) {
		setFirstname(name);
	}
	
	public int getId() {
		return id;
	}
	
	public void setId(int id) {
		this.id = id;
	}

	public void setFirstname(String firstname) {
		this.firstname = firstname;
	}

	public String getFirstname() {
		return firstname;
	}

	public void setLastname(String lastname) {
		this.lastname = lastname;
	}

	public String getLastname() {
		return lastname;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getEmail() {
		return email;
	}

	public void setAccess(Access access) {
		this.access = access;
	}
	
	public void setAccess(String access) {
		this.access = Access.ivalueOf(access);
	}

	public Access getAccess() {
		return access;
	}

	public void setReferenceId(int referenceId) {
		this.referenceId = referenceId;
	}
	
	public void setReferenceId(String referenceId) {
		this.referenceId = Integer.valueOf(referenceId);
	}

	public int getReferenceId() {
		return referenceId;
	}
	
	public static User get(int id) throws SQLException {
		Connection conn = prepareConnection();
		
		PreparedStatement stmt = conn.prepareStatement("SELECT * FROM users WHERE userId=? LIMIT 1");
		stmt.setInt(1, id);
		ResultSet rs = stmt.executeQuery();
		
		User user = null;
		if(rs.next()) {
			user = new User(rs);
		}
		
		return user;
	}
	
	public static User getByEmail(String email) throws SQLException {
		Connection conn = prepareConnection();
		
		PreparedStatement stmt = conn.prepareStatement("SELECT * FROM users WHERE email=? LIMIT 1");
		stmt.setString(1, email);
		ResultSet rs = stmt.executeQuery();
		
		User user = null;
		if(rs.next()) {
			user = new User(rs);
		}
		
		return user;
	}
	
	public static List<User> getAllActiveUsers() throws SQLException {
		Connection conn = prepareConnection();
		
		PreparedStatement stmt = conn.prepareStatement("SELECT * FROM user_log l, users u WHERE l.user_id = u.userId AND status='Active'");
		ResultSet rs = stmt.executeQuery();
		
		List<User> users = new Vector<User>();
		while(rs.next()) {
			users.add(new User(rs));
		}
		
		return users;
	}
	
	@Override
	public String toString() {
		return String.format("%s, %s", getLastname(), getFirstname());
	}
}
